// Set config defaults when creating the instance
import axios from "axios";
const isDev = process.env.NODE_ENV === "development";
const myAxios = axios.create({
    baseURL: isDev? 'http://localhost:8080/api':'http://62.234.167.239:8137/api'
});
myAxios.defaults.withCredentials = true   //向后端发请求时携带cookie
// 添加请求拦截器
myAxios.interceptors.request.use(function (config) {
    console.log("我要发请求了", config);
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
myAxios.interceptors.response.use(function (response) {
    console.log("我接收到你的响应拉", response);
    const redirectUrl = window.location.href;
    if (response.data.code == 40101) {
        window.location.href = (`/user/login?redirect=${redirectUrl}`);
    }
    // 对响应数据做点什么
    return response.data;
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});
export default myAxios;
  